package com.aspire.moss.portal.desktop;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.test.IsolatedContext;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aspire.moss.portal.R;

/**
 * 第一个单元格的内容,实现Observer观察workspace里的WorkspaceLock的变化,显示或者隐藏删除程序图标
 * 
 * @author Administrator
 * 
 */
public class AppView extends TextView implements Observer, OnClickListener {

	private static final String TAG = "AppView";

	Context mContext;

	public AppInfo appInfo; // 保存当前的单元格信息

	private static Bitmap delBitmap; // 删除图标

	private static int top;

	private int clickX, clickY;

	private boolean con;

	/**
	 * 自己的点击事件
	 */
	private OnClickListener onClickListener;

	/**
	 * 删除图标的点事件
	 */
	private OnDeleteClickListener onDeleteClickListener;

	public AppView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.AppView);
		if (null == delBitmap) {
			delBitmap = ((BitmapDrawable) a
					.getDrawable(R.styleable.AppView_delBitmap)).getBitmap();
		}
		if (top == 0) {
			top = (int) a.getDimension(R.styleable.AppView_top, 3);
		}
		super.setOnClickListener(this);
	}

	/**
	 * 更新状态是否可以点击删除图标和自己
	 */
	@Override
	public void update(Observable observable, Object data) {
		Log.d(TAG, "update(Observable observable, Object data) data=" + data);
		if (null != data && data instanceof Boolean) {
			con = (Boolean) data;
				setIcon(appInfo.iconBitmap, con);
		}
	}
	
	public void change(AppInfo appInfo){
		this.change(appInfo, VISIBLE);
	}
	
	public void change(AppInfo appInfo , int isVisible){
		this.appInfo = appInfo;
		this.setIcon(this.appInfo.iconBitmap, con);
		this.setText(this.appInfo.title);
		this.setVisibility(isVisible);
	}

	public void setIcon(Bitmap bitmap, boolean isDel) {
		Bitmap src = null;
		if (isDel) {
			src = Bitmap.createBitmap(bitmap.getWidth() + top, bitmap
					.getHeight(), Config.ARGB_8888);
			Canvas canvas = new Canvas(src);
			final int left = (bitmap.getWidth() - bitmap.getWidth()) / 2;
			canvas.drawBitmap(bitmap, left, top, null);
			canvas.save();
			canvas.drawBitmap(delBitmap, 0, 0, null);
			canvas.save();
		} else {
			src = Bitmap.createBitmap(bitmap.getWidth() + top, bitmap
					.getHeight(), Config.ARGB_8888);
			Canvas canvas = new Canvas(src);
			final int left = (bitmap.getWidth() - bitmap.getWidth()) / 2;
			canvas.drawBitmap(bitmap, left, top, null);
			canvas.save();
		}
		BitmapDrawable drawable = new BitmapDrawable(src);
		BitmapDrawable press = new BitmapDrawable(Utils.bitmapBrightness(src,
				-100));
		StateListDrawable stateListDrawable = new StateListDrawable();
		stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET, press);
		stateListDrawable.addState(View.ENABLED_FOCUSED_STATE_SET, press);
		stateListDrawable.addState(View.ENABLED_STATE_SET, drawable);
		stateListDrawable.addState(View.FOCUSED_STATE_SET, press);
		stateListDrawable.addState(View.EMPTY_STATE_SET, drawable);
		this.setCompoundDrawablesWithIntrinsicBounds(null, stateListDrawable,
				null, null);
	}

	public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG, "onTouchEvent(MotionEvent event ) event=" + event);
		clickX = (int) event.getX();
		clickY = (int) event.getY();
		return super.onTouchEvent(event);
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}

	public OnDeleteClickListener getOnDeleteClickListener() {
		return onDeleteClickListener;
	}

	public void setOnDeleteClickListener(
			OnDeleteClickListener onDeleteClickListener) {
		this.onDeleteClickListener = onDeleteClickListener;
	}

	@Override
	public void onClick(View v) {
		if (inDelete(clickX, clickY)) {
			onDeleteClickListener.onDeleteClick(AppView.this);
		} else {
			onClickListener.onClick(v);
		}
	}

	/**
	 * 判断用户是点击了删除还是本身
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private boolean inDelete(int x, int y) {
		final int delWidth = delBitmap.getWidth();
		final int delHeight = delBitmap.getHeight();
		if (x < delWidth && y < delHeight) {
			return true;
		}
		return false;
	}

	/**
	 * 点击删除图标的事件接口
	 * 
	 * @author Administrator
	 * 
	 */
	public interface OnDeleteClickListener {

		void onDeleteClick(AppView view);

	}

}

enum AppViewAction {
	MOVE, SWAP, RESET
}
